package screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.Json.Serializable;
import com.badlogic.gdx.utils.OrderedMap;
import com.badlogic.gdx.utils.Sort;
import com.badlogic.gdx.utils.SortedIntList;
import com.me.jnrgame.JnR;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Map;

import media.Score;

public class LevelCompleteWindow extends Window {

	Skin skin;
	JnR game;
	Stage stage;
	TextField nameField;
	private Map<Float, String> highScores;
	float time;

	public LevelCompleteWindow(String title, Skin _skin, JnR _game,
			Stage _stage, float _time) {
		super(title, _skin);
		Json json = new Json();

		skin = _skin;
		game = _game;
		stage = _stage;
		time = _time;
		this.setMovable(false);
		this.setSize(400, 200);
		this.setX(200);
		this.setY(140);

		Table table = new Table(skin);
		table.setWidth(this.getWidth());
		table.setHeight(this.getHeight());

		Label timeLabel = new Label("Time: " + time, skin);
		table.add(timeLabel).spaceBottom(5f);
		table.row();

		nameField = new TextField("Nickname", _skin);
		nameField.setMaxLength(10);
		table.add(nameField).size(350, 40).spaceBottom(5f);
		table.row();

		// register the button "hall of fame"
		TextButton okButton = new TextButton("Ok", skin);
		okButton.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
				game.addHighScore(nameField.getText(),
						time, game.getLevelScreen().getLevelID());

				closeWindow();

				stage.addActor(new PauseMenuWindow("WON", skin, game, game
						.getLevelScreen(), true));

			}

		});

		TextButton hallOfFameButton = new TextButton("View Hall of Fame", skin);
		hallOfFameButton.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
				closeWindow();

				stage.addActor(new HallOfFameWindow(skin, game, stage, time, 1));
			}
		});

		table.add(hallOfFameButton).size(200, 50f).uniform().spaceLeft(10)
				.spaceRight(10);
		table.row();
		table.add(okButton).size(160, 50f);

		this.add(table);
	}

	public LevelCompleteWindow(String title, Skin skin) {
		super(title, skin);
		// TODO Auto-generated constructor stub
	}

	public LevelCompleteWindow(String title, WindowStyle style) {
		super(title, style);
		// TODO Auto-generated constructor stub
	}

	public LevelCompleteWindow(String title, Skin skin, String styleName) {
		super(title, skin, styleName);
		// TODO Auto-generated constructor stub
	}

	public void closeWindow() {
		this.setVisible(false);
		this.remove();
	}


	
}
